#include "stdafx.h"

#define PLAYER_EXP
#include "Player.h"


Player::Player()
:pGraph( NULL )
,pCtrl( NULL )
,pAudio( NULL )
,pPos( NULL )
,created( false )
{
}

Player::~Player()
{
	Release();
}

bool Player::Create( wchar_t* pFileName )
{
	if( created )
		return false;

	HRESULT hr;

	hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&pGraph );
	if( FAILED(hr) )
	{
		Release();
		return false;
	}

	fileName = pFileName;
	hr = pGraph->RenderFile( fileName, NULL );
	if( FAILED(hr) )
	{
		Release();
		return false;
	}

	hr = pGraph->QueryInterface( IID_IMediaControl, (void**)&pCtrl );
	if( FAILED(hr) )
	{
		Release();
		return false;
	}

	hr = pGraph->QueryInterface( IID_IBasicAudio, (void**)&pAudio );
	if( FAILED(hr) )
	{
		Release();
		return false;
	}
	
	hr = pGraph->QueryInterface( IID_IMediaPosition, (void**)&pPos );
	if( FAILED(hr) )
	{
		Release();
		return false;
	}

	created = true;
	return true;
}

void Player::Release()
{
	if( NULL != pCtrl )
	{
		pCtrl->Release();
		pCtrl = NULL;
	}
	if( NULL != pAudio )
	{
		pAudio->Release();
		pAudio = NULL;
	}
	if( NULL != pPos )
	{
		pPos->Release();
		pPos = NULL;
	}
	if( NULL != pGraph )
	{
		pGraph->Release();
		pGraph = NULL;
	}

	created = false;
	fileName = L"";
}

bool Player::IsCreated()
{
	return created;
}

bool Player::Play()
{
	if( !created )
		return false;

	HRESULT hr = pCtrl->Run();
	if( FAILED(hr) )
		return false;

	return true;
}

bool Player::Pause()
{
	if( !created )
		return false;

	HRESULT hr = pCtrl->Pause();
	if( FAILED(hr) )
		return false;

	return true;
}

bool Player::Stop()
{
	if( !created )
		return false;

	HRESULT hr = pCtrl->Stop();
	if( FAILED(hr) )
		return false;

	hr = pPos->put_CurrentPosition( 0 );
	if( FAILED(hr) )
		return false;

	return true;
}

long Player::GetVolume()
{
	if( !created )
		return 0;

	long vol;
	pAudio->get_Volume( &vol );
	return vol;
}

bool Player::SetVolume( long volume )
{
	if( !created )
		return false;

	HRESULT hr = pAudio->put_Volume( volume );
	if( FAILED(hr) )
		return false;

	return true;
}

long Player::GetBalance()
{
	if( !created )
		return 0;

	long blc;
	pAudio->get_Balance( &blc );
	return blc;
}

bool Player::SetBalance( long balance )
{
	if( !created )
		return false;

	HRESULT hr = pAudio->put_Balance( balance ) ;
	if( FAILED(hr) )
		return false;

	return true;
}

double Player::GetTimePosition()
{
	if( !created )
		return 0;

	double time;
	pPos->get_CurrentPosition( &time );
	return time;
}

bool Player::SetTimePosition( double timePosition )
{
	if( !created )
		return false;

	HRESULT hr = pPos->put_CurrentPosition( timePosition );
	if( FAILED(hr) )
		return false;

	return true;
}

wchar_t* Player::GetFileName()
{
	wchar_t* pName = fileName.GetBuffer();
	return pName;
}
